package com.stenio.fancat.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.stenio.fancat.domain.RechOrder;
import com.stenio.fancat.domain.bo.RechIndexBO;
import com.stenio.fancat.form.RechOrderForm;
import com.stenio.fancat.pay.OrderResult;
import com.stenio.fancat.service.RechOrderService;
import com.stenio.fancat.util.ResultUtil;
import com.stenio.fancat.util.WebUtil;
import com.stenio.fancat.vo.Result;

/**
 * 充值candy模块Controller
 */
@RestController
@RequestMapping("recharge")
public class RechargeController {

	@Autowired
	private RechOrderService rechOrderService;

	/**
	 * 购买candy
	 * 
	 * @param RechOrderForm
	 * @return
	 */
	@PostMapping("buyCandy")
	@ResponseBody
	public Result buyCandy(HttpServletRequest request, @RequestBody RechOrderForm rechOrderForm) {
		OrderResult orderResult;
		try {
			if (null == rechOrderForm) {
				return ResultUtil.error("1", "参数不能为空");
			}
			rechOrderForm.setIpAddr(WebUtil.getIpAddress(request));
			orderResult = rechOrderService.saveRechOrder(rechOrderForm);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success(orderResult);
	}

	/**
	 * 获取充值首页初始化数据
	 * 
	 * @param userId
	 *            用户Id
	 * @return
	 */
	@PostMapping("initBuyCandy")
	@ResponseBody
	public Result initBuyCandy(@RequestBody RechOrderForm rechOrderForm) {
		RechIndexBO riBO;
		try {
			riBO = rechOrderService.queryRechIndex(rechOrderForm);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success(riBO);
	}

	/**
	 * 分页查询充值订单
	 * 
	 * @return
	 */
	@PostMapping("query")
	@ResponseBody
	public Result queryRechOrder(@RequestBody RechOrderForm rechOrderForm) {
		Page<RechOrder> page;
		try {
			page = rechOrderService.queryOrderByUserId(rechOrderForm);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("1", e.getMessage());
		}
		return ResultUtil.success(page);
	}

}
